#include "recentlyOpened.h"

recentlyOpened::recentlyOpened(const QString &string) : QMenu(string){}
	
void recentlyOpened::restore(QStringList &stringList){

	int i = stringList.size();
	
	for(i--; i >= 0; i--)
		addFile(stringList[i]);
	
}

QList<QAction *>::iterator recentlyOpened::getAction(QList<QAction *> &list, const QString &name) const{

	QList<QAction *>::iterator iter = list.begin();
	
	for(; iter != list.end(); iter++)
		if((*iter)->text() == name)
			return iter;
	return iter;

}

void recentlyOpened::addFile(const QString &file){

	QAction *action = new QAction(this);
	QList<QAction *> list = actions();
	QList<QAction *>::iterator iter = getAction(list, file);

	action->setText(file);
	if(iter != list.end())
		removeAction(*iter);
	else if(list.size() == MAX)
		removeAction(list[list.size() - 1]);
	
	list = actions();
	if(list.size() > 0)
		insertAction(list[0], action);
	else
		addAction(action);

}

void recentlyOpened::deleteFile(const QString &file){

	QList<QAction *> list = actions();
	QList<QAction *>::iterator iter = getAction(list, file);
	
	if(iter != list.end())
		removeAction(*iter);

}

void recentlyOpened::serialization(QStringList &stringList) const{

	QList<QAction *> list = actions();
	QList<QAction *>::iterator iter = list.begin();
	
	for(; iter != list.end(); iter++)
		stringList.push_back((*iter)->text());

}
